﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace TiebaSpider.Beans
{
    public class ThreadInformation
    {
		private Boolean _partition;

        public string TID { get; set; }
        public int TotalPage { get; set; }
        public int ReqPage { get; set; }
		public Boolean Partition {
			get
			{
				return this._partition;
			}
			set
			{
				this._partition = value;
				if (this._partition)
				{
					calculatePartitions();
				}
			} 
		}
		public int PartitionSize { get; private set; }
        public ObservableCollection<Beans.ImageInformation> ThreadImages { get; set; }
		public ObservableCollection<Tuple<int, int>> PagePartition { get; set; }
		public Tuple<int, int> currentPartition { get; set; }

        public ThreadInformation()
        {
            TID = string.Empty;
            TotalPage = 1;
            ReqPage = 1;
			Partition = false;
			PartitionSize = (App.Current as App).SetupController.AppOptions.PagePartition;
            ThreadImages = new ObservableCollection<ImageInformation>();
			PagePartition = new ObservableCollection<Tuple<int, int>>();
			currentPartition = new Tuple<int, int>(0, 0);
        }

		private void calculatePartitions()
		{
			int maxPartitions = (int)Math.Ceiling(TotalPage / (Double)PartitionSize);
			for (int i = 1; i <= maxPartitions; i++)
			{
				int startPosition = (i - 1) * PartitionSize + 1;
				int endPosition = (i == maxPartitions) ? TotalPage : (startPosition + PartitionSize - 1);
				Tuple<int, int> partition = new Tuple<int, int>(startPosition, endPosition);
				PagePartition.Add(partition);
			}
		}
	}
}
